home *** CD-ROM | disk | FTP | other *** search
- Path: nordruth.rchland.ibm.com!seurer
- From: seurer@nordruth.rchland.ibm.com (Bill Seurer)
- Newsgroups: comp.lang.modula2
- Subject: Re: Random in Modula-2 ???
- Date: 1 Apr 1996 19:49:33 GMT
- Organization: IBM Rochester MN
- Distribution: world
- Message-ID: <4jpc0d$fqb@locutus.rchland.ibm.com>
- References: <Pine.SOL.3.91.960401140010.28172E-100000@aton.abo.fi>
- Reply-To: BillSeurer@vnet.ibm.com
- NNTP-Posting-Host: nordruth.rchland.ibm.com
-
- IMPLEMENTATION MODULE Random;
- (*
- * Generates random numbers using the Lehmer algorithm, f(z)=az MOD m. The default a is 16,807 and the default m is 2,147,483,647 (2**31-1).
- * See "Random number Generators: Good Ones Are Hard To Find", Park, Stephen K., and Miller, Keith W., Communications of the ACM, October, 1988.
- *
- * In the descriptions of the procedures the bounds of the ranges are shown using:
- * [ and ] to denote an end of a range that is inclusive (i.e., IS included in the range).
- * ( and ) to denote an end of a range that is exclusive (i.e., ISN'T included in the range).
- *)
- VAR
- seed: INTEGER;
- a, m, q, r: INTEGER;
-
-
- PROCEDURE Real(): REAL;
- (*
- * Returns a random REAL number in the range [0..1)
- *)
- VAR
- lo, hi, test: INTEGER;
- BEGIN
- hi := seed DIV q;
- lo := seed MOD q;
- test := a * lo - r * hi;
- IF test > 0 THEN
- seed := test;
- ELSE
- seed := test + m;
- END (*Else*);
- RETURN FLOAT(seed) / FLOAT(m);
- END Real;
-
-
- PROCEDURE Seed(): INTEGER;
- (*
- * Returns the current seed of the random number generator
- *)
- BEGIN
- RETURN seed;
- END Seed;
-
-
- PROCEDURE SetSeed(newSeed: INTEGER);
- (*
- * Sets the seed of the random number generator
- *)
- BEGIN
- seed := newSeed;
- END SetSeed;
-
-
- PROCEDURE GetAM (VAR currA, currM: INTEGER);
- (*
- * Gets the current values for the multiplier (a) and modulus (m) used in determining the random sequence
- *)
- BEGIN
- currA := a;
- currM := m;
- END GetAM;
-
-
- PROCEDURE SetAM (newA, newM: INTEGER);
- (*
- * Sets new values for the multiplier (a) and modulus (m) used in determining the random sequence.
- *)
- BEGIN
- a := newA;
- m := newM;
- q := m DIV a;
- r := m MOD a;
- END SetAM;
-
-
- BEGIN
- SetAM (16807, 2147483647);
- SetSeed (1);
- END Random.
-
- --
-
- - Bill Seurer ID Tools and Compiler Development IBM Rochester, MN
- Business: BillSeurer@vnet.ibm.com Home: BillSeurer@aol.com
-